package com.avril.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.avril.constants.DBConstants;
import com.avril.entity.User;
import com.avril.util.ErrorManager;

public class UserDao {
	SqlManager manage = null;

	public UserDao() {
		super();
		manage = SqlManager.createInstance();
		manage.connectDB();
	}
	
	public boolean addUser(User user){
		boolean result = false;
		try {
			Object[] params = new Object[] {user.getName(),user.getPassword(),user.getFullName(),user.getPhone(),user.getUserType()};
			String sql = "insert into user(name,password,fullname,phone,utype) values(?,?,?,?,?)";
			result = manage.executeUpdate(sql, params, DBConstants.PSTM_TYPE);
			manage.closeDB();
		} catch (Exception e) {
			ErrorManager.printError("UserDao.addUser", e);
		}
		return result;
	}

	public boolean loginCheck(String username, String password) {
		boolean result = false;
		try {
			String sql = "select * from user where name=? and password=?";
			Object[] params = new Object[] { username, password };
			ResultSet rs = manage.executeQuery(sql, params, DBConstants.PSTM_TYPE);
			while (rs.next())
				result = true;
			manage.closeDB();
		} catch (Exception e) {
			ErrorManager.printError("UserDao.loginCheck", e);
		}
		return result;
	}

	public String getPower(String username) {
		String result = null;
		try {
			String sql = "select utype from user where name=?";
			Object[] params = new Object[] { username };
			ResultSet rs = manage.executeQuery(sql, params, DBConstants.PSTM_TYPE);
			rs.next(); 
			result = rs.getString("utype");
			manage.closeDB();
		} catch (Exception e) {
			ErrorManager.printError("UserDao.getPower", e);
		}
		return result;
	}
	
	public boolean modifyPassword(String userName,String password){
		boolean result=false;
		try
		{
			String sql="update user set password=? where name=?";
			Object[] params=new Object[]{password,userName};
			result=manage.executeUpdate(sql, params, DBConstants.PSTM_TYPE);
			manage.closeDB();
		}
		catch (Exception e)
		{
			ErrorManager.printError("UserDao.modifyPassword", e);
		}
		return result;
	}
	
	public String getPassword(String userName){
		String result="";
		try
		{
			String sql="select * from user where name=?";
			Object[] params=new Object[]{userName};
			ResultSet rs=manage.executeQuery(sql, params, DBConstants.PSTM_TYPE);
			while(rs.next())
			{
				result=rs.getString("password");
				break;
			}
			manage.closeDB();
		}
		catch (Exception e)
		{
			ErrorManager.printError("UserDao.getPassword", e);
		}
		return result;
	}
	
	public boolean isExist(String userName){
		boolean result = false;
		try {
			Object[] params = new Object[] { userName};
			String sql = "select * from user where name = ?";
			ResultSet rs = manage.executeQuery(sql, params, DBConstants.PSTM_TYPE);
			while (rs.next())
				result = true;
			manage.closeDB();
		} catch (Exception e) {
			ErrorManager.printError("UserDao.isExist", e);
		}
		return result;
	}
	
	public boolean deleteUser(String userName){
		boolean result=false;
		try
		{
			String sql="delete from user where name=?";
			Object[] params= new Object[]{userName};
			result=manage.executeUpdate(sql, params, DBConstants.PSTM_TYPE);
			manage.closeDB();
		}
		catch (Exception e)
		{
			ErrorManager.printError("UserDao.deleteUser", e);
		}
		return result;
	}

	public Vector<String> getAllOperators(){
		String sql = "select * from user where utype = 1";
		Vector<String> vector = new Vector<String>();
		ResultSet rs = manage.executeQuery(sql, null, DBConstants.PSTM_TYPE);
		try {
			while (rs.next()) {
				vector.add(rs.getString("name"));
			}
		} catch (SQLException e) {
			ErrorManager.printError("UserDao.getAllOperators", e);
		}
		return vector;
	}
	
	public User getUserByName(String userName){
		String sql = "select * from user where name = ?";
		Object[] params= new Object[]{userName};
		User user = null;
		ResultSet rs = manage.executeQuery(sql, params, DBConstants.PSTM_TYPE);
		try {
			rs.next();
			user = new User(rs.getString("name"),rs.getString("password"),rs.getString("fullname"),rs.getString("phone"),rs.getString("utype"));
		} catch (SQLException e) {
			ErrorManager.printError("UserDao.getUserByName", e);
		}
		return user;
	}
	
}
